const {
  MemberModel
} = require('../../../utils/member-model.js');
var utils =
  require("../../../utils/util.js");
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: "",
    lotteryData: [], //飞豆流水
    isHints: false, //暂无数据
    assetAvail: 0, //飞豆余额
    monthAdd: 0, //本月新增飞豆
    monthExpire: 0, //本月即将失效飞豆
    isLoadData: false,
    pageIndex: 1,
    pageSize: 20,
    isScroll: false,
    loadText: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      isScroll: false,
      shopId: options.shopId,
      cardNo: options.cardNo ? options.cardNo : ""
    }, function () {
      that.getUserTicketDetail();
      that.getUserAssetDetail();
    })
  },

  /**
   * 滚动到底部/右边时触发
   */
  scrollEnd: function () {
    let that = this;
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      isLoadData: true,
      loadText: "加载中..."
    }, function () {
      if (that.data.isScroll) {
        that.setData({
          isLoadData: true,
          loadText: "已经到底了"
        });
      } else {
        that.getUserTicketDetail();
      }
    })
  },

  /**
   * 会员资产详情
   */
  getUserTicketDetail() {
    let that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.shopId = that.data.shopId; //门店id
    param.cardNo = that.data.cardNo; //卡号
    param.offset = (that.data.pageIndex - 1) * that.data.pageSize;
    param.limit = that.data.pageSize;
    memberModel.userTicketDetail(param).then(data => {
      if (data) {
        if (data.data.length < that.data.pageSize) {
          that.setData({
            isScroll: true
          })
        }
        if (data.data && data.totalCount > 0) {
          var ticketDetailData = [];
          for (var i = 0; i < data.data.length; i++) {
            var list = {};
            var item = data.data[i];
            list.happenTime = item.happenTime ? item.happenTime.replace(/-/g, "/") : "" || ""; //时间
            list.lotteryNum = item.lotteryNum ? item.lotteryNum : 0; //飞豆数量
            list.access = (item.operType == 0 || item.operType == 2) ? "+" : (item.operType == 1 || item.operType == 3) ? "-" : "" //0存票 1取票
            list.operation = item.operation; //操作类型
            ticketDetailData.push(list);
          }
          // 将新一页的数据添加到原数据后面
          var newTicketDeta = that.data.lotteryData;
          var newTicketList = newTicketDeta.concat(ticketDetailData);
          that.setData({
            isLoadData: false,
            isHints: false,
            lotteryData: newTicketList
          })
        } else {
          that.setData({
            loadText: "",
            isLoadData: false,
            isHints: true,
            lotteryData: []
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 会员资产详情
   */
  getUserAssetDetail() {
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetType = 1; //飞豆
    param.statistics = true; //是否查询统计数据
    param.assetStatus = 0; //0:正常 3:过期
    param.shopId = this.data.shopId; //门店id
    memberModel.userAssetDetail(param).then(data => {
      var that = this;
      that.setData({
        assetAvail: utils.formatCurrency(data.assetAvail) || 0, //飞豆余额
        monthAdd: utils.formatCurrency(data.monthAdd) || 0, //本月新增飞豆
        monthExpire: utils.formatCurrency(data.monthExpire) || 0, //本月即将失效飞豆
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})